//
//  MYY_walletViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/26.
//  Copyright © 2017年 hbjApple. All rights reserved.
// 我的钱包

#import "MYY_walletViewController.h"
#import "LuckyMoneyVocherController.h"

#import "MYY_walletViewCell.h"
#import "MYY_walletTopView.h"
#import "MYY_walletNavView.h"
#import "MYXRechargeViewController.h"
#import "MYXbalanceViewController.h"
#import "MYXRedListController.h"
#import "MYXmymoneyViewModel.h"
@interface MYY_walletViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>{
     CGFloat lastOriginalY;
    MYXmymoneyViewModel *MYXmymoneyViewM;
}
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy)  NSMutableArray *categorytitleArray;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong)MYY_walletTopView *MYY_walletTopV;
@property (nonatomic, strong)MYY_walletNavView *MYY_walletNavVi;

@end

@implementation MYY_walletViewController
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self initMymoney];
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
//导航栏
-(void)initNavgationBar
{
    
     WS(weakSelf)
    self.MYY_walletNavVi =[[MYY_walletNavView alloc]init];
    self.MYY_walletNavVi.frame = CGRectMake(0, 0, _tableView.frame.size.width,SafeAreaTopHeight);
    [self.view addSubview:self.MYY_walletNavVi];
    [self.MYY_walletNavVi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@(SafeAreaTopHeight));
    }];
    [self.MYY_walletNavVi.rightBtn setTitle:@"明细" forState:UIControlStateNormal];

    self.MYY_walletNavVi.backButton =^{
        [weakSelf.navigationController popViewControllerAnimated:YES];

    };
    self.MYY_walletNavVi.rightButton =^{
        MYXRedListController *MYXRedList=[[MYXRedListController alloc]init];
        MYXRedList.navigationItem.title = @"余额明细";
        [weakSelf.navigationController pushViewController:MYXRedList animated:YES];

        
    };
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    AdjustsScrollViewInsetNever(self, self.tableView);
    if (MYXmymoneyViewM == nil) {
        MYXmymoneyViewM = [[MYXmymoneyViewModel alloc] init];
    }
    [self createUI];
}
#pragma mark - UI

- (void)createUI
{
     WS(weakSelf)
   // [self initNavgationBar];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
    self.MYY_walletTopV.RechargeB =^{

        MYXRechargeViewController *MYXRechargeVC =[[MYXRechargeViewController alloc]init];
        MYXRechargeVC.navigationItem.title = @"充值";
        [weakSelf.navigationController pushViewController:MYXRechargeVC animated:YES];
    };
    
    
    [self initNavgationBar];
   
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        self.MYY_walletTopV=[[MYY_walletTopView alloc]init];
        self.MYY_walletTopV.frame = CGRectMake(0, 0, _tableView.frame.size.width,260);
        _tableView.tableHeaderView =self.MYY_walletTopV;
        
    }
    return _tableView;
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYY_walletViewCell *MYY_walletViewCe = [MYY_walletViewCell cellWithTableView:tableView];
    MYY_walletViewCe.backgroundColor =[UIColor whiteColor];
    if (indexPath.section==0) {
        MYY_walletViewCe.nameView.text = @"余额";
        if ([MYXmymoneyViewM.viewModel.money floatValue]>0) {
            MYY_walletViewCe.contentLable.text =  [NSString stringWithFormat:@"%@元",MYXmymoneyViewM.viewModel.money];
        }else{
            MYY_walletViewCe.contentLable.text =  [NSString stringWithFormat:@"0元"];
        }
       
    }else if (indexPath.section==1){
        MYY_walletViewCe.nameView.text = @"红包现金劵";
        if ([MYXmymoneyViewM.viewModel.ticketcount intValue]>0) {
            MYY_walletViewCe.contentLable.text = [NSString stringWithFormat:@"%@张",MYXmymoneyViewM.viewModel.ticketcount];
        }else{
             MYY_walletViewCe.contentLable.text = [NSString stringWithFormat:@"0张"];
        }
       
        
    }
    
    return MYY_walletViewCe;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    headView.backgroundColor =MYYColorFromRGB(0xf2f2f2);
    
    return headView;
}
-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *fooerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.1)];
    fooerView.frame =CGRectMake(0, 0, WIDTH, 0.1);
    fooerView.backgroundColor =MYYColorFromRGB(0xf2f2f2);
    return fooerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        MYXbalanceViewController *MYXbalanceVC =[[MYXbalanceViewController alloc]init];
        MYXbalanceVC.navigationItem.title = @"充值";
        [self.navigationController pushViewController:MYXbalanceVC animated:YES];
        
    }else if (indexPath.section==1){
         if ([MYXmymoneyViewM.viewModel.ticketcount intValue]>0) {
             LuckyMoneyVocherController *vocherVC = [[LuckyMoneyVocherController alloc] init];
        
             [self.navigationController pushViewController:vocherVC animated:YES];
         }else{
             [JXTHUDHelper showHUDText:@"您还没有红包劵" type:@"error" inView:nil];
         }
    }
}

#pragma mark -  我的钱包接口


-(void)initMymoney{
    [MYXmymoneyViewM loadmymoney:^(BOOL isSucess) {
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
